package com.ht.common.core.utils;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.hibernate.validator.HibernateValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ht.common.core.exception.ServiceException;

/**
 * 参数校验
 * @author Administrator
 *
 */
public class MsgValidUtils {
	protected final static Logger log = LoggerFactory.getLogger(MsgValidUtils.class);
	
	/**
	 * 校验参数是否正确
	 * @param msgObject
	 */
	public static void validMsgContent(Object msgObject , Class<?>... groups) {
		if(null==msgObject){
			return;
		}
		ValidatorFactory vf = Validation.byProvider(HibernateValidator.class).configure()// 快速失败模式
	            .failFast(true)
	            .buildValidatorFactory();
		Validator validator = vf.getValidator();
		Set<ConstraintViolation<Object>> set = validator.validate(msgObject);
		for (ConstraintViolation<Object> constraintViolation : set) {
			String errMsg=constraintViolation.getPropertyPath().toString()+":"+constraintViolation.getMessage();
			if(StringUtils.isBlank(errMsg)){
				throw new ServiceException("报文参数错误");
			}
			log.info("-=={}",constraintViolation.getPropertyPath());
		   throw new ServiceException(errMsg);
		}
	}
	
}
